//package com.photoeditor.demo.ad;
//
//import android.app.Activity;
//import android.content.Context;
//import android.text.TextUtils;
//
//import com.common.base.ComponentContext;
//import com.common.base.app.BaseApplication;
//import com.common.base.log.RLog;
//import com.common.base.utils.DebugUtil;
//import com.common.commercialize.ad.AdDisplay;
//import com.common.commercialize.ad.AdManager;
//import com.common.commercialize.ad.third.IAd;
//import com.common.commercialize.ad.third.ThirdAdListener;
//import com.common.commercialize.sdk.CommercializeSDK;
//import com.common.commercialize.utils.SceneStatisticsNew;
//import com.photoeditor.demo.model.event.DownloadFinishEvent;
//import com.photoeditor.demo.model.store.download.DownloadUtils;
//import com.photoeditor.demo.model.store.download.IDownloadListener;
//import com.photoeditor.demo.model.store.module.BaseOnlineManager;
//import com.photoeditor.demo.model.store.module.StoreOnlineBean;
//import com.photoeditor.demo.statistics.StatisticsConstant;
//import com.photoeditor.demo.statistics.StatisticsUtils;
//import com.photoeditor.demo.ui.dialog.DownloadAdDialog;
//
//import org.greenrobot.eventbus.EventBus;
//
//import java.util.concurrent.ConcurrentLinkedQueue;
//
//import static com.photoeditor.demo.ad.AdProvider.REASON_PAGE_NO_SHOWING;
//
///**
// * Created by mutour on 2017/8/15.
// */
//
//public class DownloadAdProvider {
//    private static final String TAG = "store_download";
//    private boolean DEBUG = CommercializeSDK.TestUser.isTestUser() || DebugUtil.isDebuggable();
//    private String mUnitId;
//
//    private int mFunId;
//
//    private ConcurrentLinkedQueue<IDownloadListener> mListeners = new ConcurrentLinkedQueue<>();
////    // 当前正在下载的
////    private ConcurrentSkipListSet<String> mDownloadingId = new ConcurrentSkipListSet<>();
//
//    private static DownloadAdProvider instance;
//    private AdItemBean mAdItemBean;
//    private int adTouchType = AdDisplay.AdTouchType.DEFAULT;
//
//    public static DownloadAdProvider getInstance() {
//        if (instance == null) {
//            synchronized (DownloadAdProvider.class) {
//                if (instance == null) {
//                    instance = new DownloadAdProvider();
//                }
//            }
//        }
//        return instance;
//    }
//
//    public DownloadAdProvider() {
//        DEBUG = DebugUtil.isDebuggable() || CommercializeSDK.TestUser.isTestUser();
//        mUnitId = AdKeyConstant.InappAdPlacementID.DOWNLOAD_ID;
//        mFunId = -1;
//    }
//
//    private void clearListener() {
//        for (IDownloadListener listener : mListeners) {
//            DownloadUtils.getInstance().removeListener(listener);
//        }
//        mListeners.clear();
//    }
//
//    public synchronized void addListener(StoreOnlineBean bean, int type) {
//        IDownloadListener downListener = new DownloadListener(bean, type);
//        if (!mListeners.contains(downListener)) {
//            DownloadUtils.getInstance().addListener(downListener);
//            mListeners.add(downListener);
//            if (mAdItemBean != null && mAdItemBean.hasAdAndInvalid()) {
//                destroyDownloadAd();
//            }
//            loadDownloadAd(bean.getName());
//        }
//    }
//
//    private void removeListener(IDownloadListener listener) {
////        DownloadUtils.getInstance().removeListener(listener);
//        mListeners.remove(listener);
//    }
//
//    private synchronized void loadDownloadAd(final String name) {
////        if (SVipManager.getInstance().isSVipUser()) {
////            statisticsNoRequestAd(name, AdProvider.REASON_SVIP_USER);
////            return;
////        }
//
//        if(mAdItemBean != null && mAdItemBean.hasAd() && !mAdItemBean.hasAdAndInvalid() && DownloadAdDialog.hasDialog()) {
//            DownloadAdDialog.refreshAd(mAdItemBean);
//            return;
//        }
//        Context context = ComponentContext.getContext();
//
////        InappStrategyBean.InappStrategyConfig config = InappAdManager.getInstance().getStrategyConfig(mFunId);
////        String[] falseReason = new String[1];
////        if (!InappAdManager.getInstance().needRequest(mFunId, mUnitId, "download_ad", "", config, falseReason)) {
////            statisticsNoRequestAd(name, AdProvider.REASON_PROTOCOL_UN_FIT + ":" + falseReason[0]);
////            return;
////        }
//
////        if (config != null) {
////            adTouchType = config.getAdTouchType();
////            mUnitId = config.getAdUnitId(0, mUnitId);
////        } else {
//            adTouchType = AdDisplay.AdTouchType.DEFAULT;
////        }
//
//        final AdManager.AdRequest request = AdManager.getInstance().createRequest();
//        request.setSceneName("download_ad")
//                .addAdmobBannerAd(context, 280, 250)
//                .addAdmobNativeAd(context, 280, 250)
//                .setBatmobiUnitId(mUnitId)
//                .setAdListener(new ThirdAdListener() {
//                    @Override
//                    public void onAdRequest(IAd var1) {
//                        if (DEBUG) {
//                            RLog.d(TAG, "start request ad:" + (var1 != null ? var1.getPlacementId() : ""));
//                        }
//                    }
//
//                    @Override
//                    public void onAdClose(IAd var1) {
//
//                    }
//
//                    @Override
//                    public void onError(IAd ad, String s) {
//                        if (DEBUG) {
//                            RLog.d(TAG, "error request ad:" + s);
//                        }
//                        if (CommercializeSDK.TestUser.isTestUser() || DebugUtil.isDebuggable()) {
//                            if (!TextUtils.isEmpty(mUnitId)) {
//                                String adCacheKey = request.getKey();
//                                TestAd testAd = new TestAd(adCacheKey);
//                                testAd.loadInternal();
//                                testAd.setAdListener(this);
//                                AdManager.getInstance().putAd(adCacheKey, testAd);
//                                onAdLoaded(testAd);
//                                return;
//                            }
//                        }
//                        destroyDownloadAd();
//
//                        if (DownloadAdDialog.hasDialog() && !DownloadAdDialog.isAttachedAd()) {
////                            if (AdPreloadManager.getInstance().hasNativeAd()) {
////                                String adCacheKey = AdPreloadManager.getInstance().getNativeAdCache(this, "download");
////                                if (!TextUtils.isEmpty(adCacheKey)) {
////                                    mAdItemBean = new AdItemBean(adCacheKey);
////                                    mAdItemBean.setAdTouchType(adTouchType);
////                                    DownloadAdDialog.refreshAd(mAdItemBean);
////                                }
////                            }
//                        }
//                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_FALID,
//                                "name", name,
//                                "error", s);
//                    }
//
//                    @Override
//                    public void onAdLoaded(IAd ad) {
//                        if (DEBUG) {
//                            RLog.d(TAG, "finish ad:" + ad.getPlacementId());
//                        }
//                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_FILL,
//                                "name", name,
//                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//                        String adCacheKey = request.getKey();
//                        if (adCacheKey != null) {
//                            mAdItemBean = new AdItemBean(adCacheKey);
//                            mAdItemBean.setAdTouchType(adTouchType);
//                            if (DownloadAdDialog.hasDialog() && !DownloadAdDialog.isAttachedAd()) {
//                                DownloadAdDialog.refreshAd(mAdItemBean);
//                            }
//                        }
//                    }
//
//                    @Override
//                    public void onAdClicked(IAd ad) {
//                        if (DEBUG) {
//                            RLog.d(TAG, "clicked ad:" + ad.getPlacementId());
//                        }
//                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_CLICK,
//                                "name", name,
//                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//                    }
//
//                    @Override
//                    public void onLoggingImpression(IAd ad) {
//                        if (DEBUG) {
//                            RLog.d(TAG, "show ad:" + ad.getPlacementId());
//                        }
//                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_SHOW,
//                                "name", name,
//                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_STATE, String.valueOf(true),
//                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//                    }
//                });
//
//        AdManager.getInstance().loadAd(ComponentContext.getContext(), request);
//        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_REQUEST,
//                "name", name,
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_STATE, String.valueOf(true),
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//    }
//
//    public void statisticsNoRequestAd(String name, String reason) {
//        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_REQUEST,
//                "name", name,
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_STATE, String.valueOf(false),
//                "reason", reason == null ? "un_know" : reason,
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//    }
//
//    public void statisticsNoShowAd(String reason) {
//        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_SHOW,
//                "reason", reason == null ? "un_know" : reason,
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_STATE, String.valueOf(false),
//                "hasAd", String.valueOf(mAdItemBean != null && mAdItemBean.hasAd()),
//                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
//    }
//
//    public void showDownloadAd(Activity mActivity, final StoreOnlineBean storeOnlineBean, final OnActionListener actionListener) {
//        if (mActivity == null || BaseApplication.isBackground()) {
//            if (DebugUtil.isDebuggable() || CommercializeSDK.TestUser.isTestUser()) {
//                RLog.v(TAG, "activity is null or app is back ground");
//            }
//            statisticsNoShowAd(REASON_PAGE_NO_SHOWING);
//            return;
//        }
//        String adCache = null;
//        int touchType = -1;
//        if(mAdItemBean != null && mAdItemBean.hasAd()) {
//            adCache = mAdItemBean.getAdCacheKey();
//            touchType = mAdItemBean.getAdTouchType();
//        } else {
////            if (AdPreloadManager.getInstance().hasNativeAd()) {
////                adCache = AdPreloadManager.getInstance().getNativeAdCache(new ThirdAdListener() {
////                    @Override
////                    public void onAdClicked(Ad ad) {
////                        if (DEBUG) {
////                            RLog.d(TAG, "clicked ad:" + ad.getPlacementId());
////                        }
////                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_CLICK,
////                                "name", storeOnlineBean.getName(),
////                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ADSOURCE, AdManager.getAdSourceDescription(AdManager.getInstance().getAdType(ad)),
////                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
////                    }
////
////                    @Override
////                    public void onLoggingImpression(Ad ad) {
////                        if (DEBUG) {
////                            RLog.d(TAG, "show ad:" + ad.getPlacementId());
////                        }
////                        DownloadAdDialog.statistics(SceneStatisticsNew.EVENT_SUFFIX_AD_SHOW,
////                                "name", storeOnlineBean.getName(),
////                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_STATE, String.valueOf(true),
////                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ADSOURCE, AdManager.getAdSourceDescription(AdManager.getInstance().getAdType(ad)),
////                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_UID, mUnitId);
////                    }
////                }, "download");
////                touchType = this.adTouchType;
////            }
//        }
//        DownloadAdDialog.show(mActivity, storeOnlineBean, adCache, touchType, actionListener);
//    }
//
//    public void destroyDownloadAd() {
//        if (mAdItemBean != null) {
//            mAdItemBean.destory();
//            mAdItemBean = null;
//        }
//    }
//
//    public abstract static class OnActionListener implements DownloadAdDialog.OnActionListener {
//        @Override
//        public void onDismiss(boolean clickApply, boolean clickClose) {
//            getInstance().destroyDownloadAd();
//        }
//    }
//
//    private class DownloadListener implements IDownloadListener {
//        private static final int DOWNLOAD_UNKNOWN = 0;
//        private static final int DOWNLOAD_START = 1;
//        private static final int DOWNLOAD_FINISH = 2;
//        private static final int DOWNLOAD_FAILED = 3;
//        private int mState = DOWNLOAD_UNKNOWN;
//
//        private String pkgName;
//        private String name;
//        private StoreOnlineBean bean;
//        int type;
//
//        public DownloadListener(StoreOnlineBean bean, int type) {
////            if (type == BaseOnlineManager.STORE_FUNC_TYPE_FILTER) {
////                pkgName = bean.getId();
////            } else {
//                pkgName = bean.getPkgName();
////            }
//            name = bean.getName();
//            this.bean = bean;
//            this.type = type;
//        }
//
//        @Override
//        public boolean equals(Object obj) {
//            if (obj != null && obj instanceof DownloadListener) {
//                return TextUtils.equals(pkgName, ((DownloadListener) obj).getPkgName())
//                        && type == ((DownloadListener) obj).getType();
//            }
//            return super.equals(obj);
//        }
//
//        @Override
//        public int hashCode() {
//            return pkgName.hashCode() << type;
//        }
//
//        @Override
//        public String getDownloadId() {
//            return pkgName;
//        }
//
//        @Override
//        public void onProgressUpdate(String downloadId, int progress) {
//            if (!TextUtils.equals(downloadId, pkgName)) {
//                return;
//            }
//            if (progress == 0) {
//                if (mState != DOWNLOAD_START) {
//                    if (DEBUG) {
//                        RLog.d(TAG, "start download:" + downloadId);
//                    }
////                    mDownloadingId.add(downloadId);
//                    mState = DOWNLOAD_START;
//                }
//            } else if (progress == 100) {
//                StatisticsUtils.statisicsCustom(StatisticsConstant.UMENG_CUSTOM_EVENT_PREFIX_FUNCTION, "download_state",
//                        "event", "success");
//                if (mState != DOWNLOAD_FINISH) {
//                    // 开始展示广告
//                    if (DEBUG) {
//                        RLog.d(TAG, "finish download:" + downloadId);
//                    }
//                    EventBus.getDefault().post(new DownloadFinishEvent(bean, type));
////                    mDownloadingId.remove(downloadId);
//                    removeListener(DownloadListener.this);
//                    mState = DOWNLOAD_FINISH;
//                }
//            }
//        }
//
//        @Override
//        public void onFailed(String packageName) {
//            if (DEBUG) {
//                RLog.d(TAG, "error download:" + packageName);
//            }
//
//            StatisticsUtils.statisicsCustom(StatisticsConstant.UMENG_CUSTOM_EVENT_PREFIX_FUNCTION, "download_state",
//                    "event", "failed");
////            mDownloadingId.remove(pkgName);
//            removeListener(DownloadListener.this);
//            mState = DOWNLOAD_FAILED;
//        }
//
//        @Override
//        public String getFullActivityName() {
//            return null;
//        }
//
//        public String getPkgName() {
//            return pkgName;
//        }
//
//        public int getType() {
//            return type;
//        }
//    }
//}
